// pages/mykanj/mykanj.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
let wpxtimes=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    clickIndex:null,
    open:false,
    wpxIos:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      wpxIos: getApp().globalData.wpxIos,
    })
  },


  getList(){
    let that=this
    this.data.list.forEach((ele,index) => {
      clearInterval(wpxtimes[index])
    });
    this.setData({
      list:[]
    })
    JsonPost('bargaining/goodsbargaininglist').then(res=>{
      let arr=JSON.parse(JSON.stringify(res.data))
      arr.forEach((ele,index) => {
        if (new Date().getTime() - (ele.addtime*1000)>86400000) {
          arr[index].wpxStatus=0
        }else{
          arr[index].wpxStatus=1
        }
      });
      this.setData({
        list:arr
      })
      arr.forEach((ele, index) => {
        if (ele.wpxStatus==0) {
          return
        }
        let str = "list[" + index + "].addtime"
        that.setData({
          [str]:((ele.addtime*1000)+(24 * 3600000))-new Date().getTime()
        })
          let d = null;
          let h = null;
          let m = null;
          let s = null;
          that.setData({
            [str]: that.data.list[index].addtime - 1
          })
          let t = that.data.list[index].addtime;
          d = Math.floor(t / (24 * 3600));
          h = Math.floor((t - 24 * 3600 * d) / 3600);
          m = Math.floor((t - 24 * 3600 * d - h * 3600) / 60);
          s = Math.floor((t - 24 * 3600 * d - h * 3600 - m * 60));
          let dd = "list[" + index + "].dd"
          let hh = "list[" + index + "].hh"
          let mm = "list[" + index + "].mm"
          let ss = "list[" + index + "].ss"
          that.setData({
            [dd]: d,
            [hh]: h,
            [mm]: m,
            [ss]: s
          })
          if (that.data.list[index].addtime <= 0) {
            let str = "list[" + index + "].addtime"
            that.setData({
              [str]: 0
            })
            clearInterval(wpxtimes[index])
          } else {
              wpxtimes[index] = setInterval(() => {
                let d = null;
                let h = null;
                let m = null;
                let s = null;
                let str = "list[" + index + "].addtime"
                that.setData({
                  [str]: that.data.list[index].addtime - 1
                })
                let t = that.data.list[index].addtime;
                d = Math.floor(t / (24 * 3600));
                h = Math.floor((t - 24 * 3600 * d) / 3600);
                m = Math.floor((t - 24 * 3600 * d - h * 3600) / 60);
                s = Math.floor((t - 24 * 3600 * d - h * 3600 - m * 60));
                let dd = "list[" + index + "].dd"
                let hh = "list[" + index + "].hh"
                let mm = "list[" + index + "].mm"
                let ss = "list[" + index + "].ss"
                that.setData({
                  [dd]: d,
                  [hh]: h,
                  [mm]: m,
                  [ss]: s
                })
                if (that.data.list[index].addtime <= 0) {
                  let str = "list[" + index + "].addtime"
                  that.setData({
                    [str]: 0
                  })
                  clearInterval(wpxtimes[index])
                }
              }, 1000);

          }

      });
    })
  },

  goPay(e){
    this.setData({
      clickIndex:e.currentTarget.dataset.index
    })
    this.selectComponent("#formatBox").getInfo()
    this.setData({
      open:true
    })
  },

  
  _getGuiGObj(e) {
    let specInfo= e.detail
    let arr = [Object.assign(specInfo, this.data.list[this.data.clickIndex])]
      arr[0].shop_price = arr[0].price
      getApp().globalData.shopCartInfo=arr
      wx.navigateTo({
        url: '/pages/orderPay/orderPay?ifCart=false&bargaining_id='+this.data.list[this.data.clickIndex].self_id
      })
  },

  goOn(e){
    wx.navigateTo({
      url: '/pages/yaoKan/yaoKan?id='+e.currentTarget.dataset.id+'&self_id='+e.currentTarget.dataset.self_id+'&ifKan=true',
    })
  },

  goGood(e){
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goods_id='+e.currentTarget.dataset.id,
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.data.list.forEach((ele,index) => {
      clearInterval(wpxtimes[index])
    });
    this.setData({
      list:[]
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.data.list.forEach((ele,index) => {
      clearInterval(wpxtimes[index])
    });
    this.setData({
      list:[]
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})